//
//  TableViewHeader.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/19.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "TableViewHeader.h"
#import "TopCollectionView.h"

@interface TableViewHeader ()

@property (nonatomic, strong)TopCollectionView * topView;
@property (nonatomic, weak)UILabel * bigLabel;
@property (nonatomic, weak)UIImageView * dateImageView;
@property (nonatomic, weak)UILabel * dateLabel;

@end

@implementation TableViewHeader

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = ZRWhiteColor;
    
    self.topView = [[TopCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 170 * SCREENH6)];
    self.topView.type = @"bana";
    
    [self addSubview:self.topView];
    
    UILabel * bigLabel = [UILabel new];
    
    bigLabel.text = [NSString stringWithFormat:@"今日\n玩什么?"];
    bigLabel.textColor = ZRBlackColor;
//    bigLabel.font = [UIFont boldSystemFontOfSize:30 * SCREENH6];
    bigLabel.font = [UIFont fontWithName:@"HYk2gj" size:35 * SCREENH6];
    bigLabel.numberOfLines = 2;
    
    self.bigLabel = bigLabel;
    
    [self addSubview:self.bigLabel];
    
    UIImageView * dateImageView = [UIImageView new];
    
    dateImageView.image = [UIImage imageNamed:@"calendar"];
    
    self.dateImageView = dateImageView;
    
    [self addSubview:self.dateImageView];
    
    //获取当前时间日期
    NSDate *date=[NSDate date];
    NSDateFormatter *format1=[[NSDateFormatter alloc] init];
    [format1 setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr;
    dateStr=[format1 stringFromDate:date];
    
    NSString * dateString = [self getWeekDay:dateStr];
    
    UILabel * dateLabel = [UILabel new];
    
    dateLabel.text = dateString;
    dateLabel.textColor = ColorWithRGB(0x909090);
    dateLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
    
    self.dateLabel = dateLabel;
    
    [self addSubview:self.dateLabel];
}


- (void)layoutSubviews{
    
    [self.bigLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(25 * SCREENH6);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.dateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.bigLabel);
        make.top.mas_equalTo(self.bigLabel.mas_bottom).offset(5 * SCREENH6);
        make.height.width.mas_equalTo(12 * SCREENH6);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dateImageView);
        make.leading.mas_equalTo(self.dateImageView.mas_trailing).offset(10 * SCREENH6);
    }];
}


- (NSString*)getWeekDay:(NSString*)currentStr
{
    
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];//实例化一个NSDateFormatter对象
    
    [dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式,要注意跟下面的dateString匹配，否则日起将无效
    
    NSDate*date =[dateFormat dateFromString:currentStr];
    
    NSArray*weekdays = [NSArray arrayWithObjects: [NSNull null],@"星期天",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",nil];
    
    NSCalendar*calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone*timeZone = [[NSTimeZone alloc]initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit =NSCalendarUnitWeekday;
    
    NSDateComponents * theComponents = [calendar components:calendarUnit fromDate:date];
    
    NSArray *array = [currentStr componentsSeparatedByString:@"-"];
    
    int dateValue = [array[1] intValue];
    NSString * dateString;
    switch (dateValue) {
        case 1:
            dateString = @"一月";
            break;
        case 2:
            dateString = @"二月";
            break;
        case 3:
            dateString = @"三月";
            break;
        case 4:
            dateString = @"四月";
            break;
        case 5:
            dateString = @"五月";
            break;
        case 6:
            dateString = @"六月";
            break;
        case 7:
            dateString = @"七月";
            break;
        case 8:
            dateString = @"八月";
            break;
        case 9:
            dateString = @"九月";
            break;
        case 10:
            dateString = @"十月";
            break;
        case 11:
            dateString = @"十一月";
            break;
        case 12:
            dateString = @"十二月";
            break;
        default:
            break;
    }
    
    NSString * weekday = [weekdays objectAtIndex:theComponents.weekday];
    NSString * dayString = [NSString stringWithFormat:@"%@",array.lastObject];
    
    NSString * rest = [NSString stringWithFormat:@"%@ ,%@ %@",weekday,dayString,dateString];
    
    return rest;
    
}

@end
